package com.tanhua.app.controller;

import com.tanhua.app.service.UserInfoService;
import com.tanhua.app.service.UserService;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.pojo.UserInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 发送验证码的短信
     * @param param
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map<String ,String> param){

        //1.获取手机号
        String phone = param.get("phone");

        //2.对该手机号发送短息
        userService.sendSms(phone);

        //3.响应数据
        return ResponseEntity.ok(null);
    }


    /**
     * 登录功能——验证码校验
     * @param param
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map param){
        //1.获取参数(手机号，验证码)
        String phone = (String) param.get("phone");
        String code = (String) param.get("verificationCode");

        //2.调用Service层执行业务逻辑（执行后需返回的信息是：是否是新用户和token）
        Map result = userService.loginVerification(phone,code);

        //3.返回结果
        return ResponseEntity.ok(result);
    }


    /**
     * 用户登录后，保存用户的基本信息和图像信息
     * @param userInfo
     * @param token
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization")String token){
        //1.校验是否是该用户的信息（token是否正确）
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken){
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }
        //2.获取用户id,保存到userInfo对象中
        Claims claims = JwtUtils.getClaims(token);
        Long id = Long.parseLong(claims.get("id").toString());
        userInfo.setId(id);
        //3.通过Service层执行业务
        userInfoService.save(userInfo);
        //4.返回结果
        return ResponseEntity.ok(null);
    }


    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto, @RequestHeader("Authorization")String token) throws IOException {
        //1.判断token是否是正确的
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken){
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }

        //2.获取用户id,根据用户idd保存到图片
        Claims claims = JwtUtils.getClaims(token);
        long id = Long.parseLong(claims.get("id").toString());

        //3.调用Service层的方法执行业务逻辑
        userInfoService.updateHead(headPhoto,id);

        //4.返回结果
        return ResponseEntity.ok(null);
    }

}
